package com.real8.romemp.client.activity;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.real8.romemp.client.ClientFactory;
import com.real8.romemp.client.place.PiracyPlace;
import com.real8.romemp.client.place.TradePlace;
import com.real8.romemp.client.ui.PiracyView;
import com.real8.romemp.client.ui.PiracyView.Presenter;
import com.real8.romemp.shared.GameData;

public class PiracyActivity extends AbstractActivity implements Presenter {
	private final ClientFactory clientFactory;
	private PiracyView view;

	public PiracyActivity(PiracyPlace place, ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}

	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view = clientFactory.getPiracyView();
		if (view != null) {
			view.setPresenter(this);

			// prepare view
			view.hideAll();
			view.showPiratesAttack();

			// determine & set local losses
			GameData gameData = clientFactory.getGameData();
			int RL = Random.nextInt(50) + 50;
			int RP_localMerchantShipsLost = (RL - gameData
					.getWL_localWarShips()) / 10;
			if (RP_localMerchantShipsLost < 0) {
				RP_localMerchantShipsLost = 0;
			}
			int ML_localMerchantShips = gameData.getML_localMerchantShips();
			if (RP_localMerchantShipsLost > ML_localMerchantShips) {
				RP_localMerchantShipsLost = ML_localMerchantShips;
			}
			gameData.setML_localMerchantShips(ML_localMerchantShips
					- RP_localMerchantShipsLost);
			gameData.setMS_merchantShips(gameData.getMS_merchantShips()
					- RP_localMerchantShipsLost);
			gameData.setLM_labourMerchants(gameData.getLM_labourMerchants()
					- RP_localMerchantShipsLost);

			// determine & set oversee losses
			int RO = Random.nextInt(50) + 50;
			int RQ_overseeMerchantShipsLost = (RO - gameData
					.getWO_overseeWarShips()) / 10;
			if (RQ_overseeMerchantShipsLost < 0) {
				RQ_overseeMerchantShipsLost = 0;
			}
			int MO_overseeMerchantShips = gameData.getMO_overseeMerchantShips();
			if (RQ_overseeMerchantShipsLost > MO_overseeMerchantShips) {
				RQ_overseeMerchantShipsLost = MO_overseeMerchantShips;
			}
			gameData.setMO_overseeMerchantShips(MO_overseeMerchantShips
					- RQ_overseeMerchantShipsLost);
			gameData.setMS_merchantShips(gameData.getMS_merchantShips()
					- RQ_overseeMerchantShipsLost);
			gameData.setLM_labourMerchants(gameData.getLM_labourMerchants()
					- RQ_overseeMerchantShipsLost);

			// show losses
			view.showLosses(RP_localMerchantShipsLost,
					RQ_overseeMerchantShipsLost);

			// show continue
			view.showContinue();

			// set widget
			containerWidget.setWidget(view.asWidget());
		}
	}

	@Override
	public void onContinue() {
		goToNextPlace();
	}

	private void goToNextPlace() {
		clientFactory.getPlaceController().goTo(new TradePlace());
	}
}
